public class ThreadDemo29 {
    public static void main(String[] args) throws InterruptedException {

        Object A = new Object();
        Object B = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (A) {
                System.out.println("t1尝试持有锁A");
                try {
                    // 休眠1s，保证t2能拿到锁B
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (B) {
                    System.out.println("t1尝试持有锁B");
                }
            }


        });

        Thread t2 = new Thread(() -> {
            synchronized (B) {
                System.out.println("t2尝试持有锁B");
                try {
                    // 休眠1s，保证t1能拿到锁A
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (A) {
                    System.out.println("t2尝试持有锁A");
                }
            }


        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }
}
